package com.hnhs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnhs.entity.UserQuery;
import com.hnhs.entity.Userd;
import com.hnhs.mapper.UserdMapper;
import com.hnhs.service.IUserdService;
import com.hnhs.utils.ResultUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yknt
 * @since 2024-07-19
 */
@RestController
@RequestMapping("/api/userFind")
public class UserdController {

    @Autowired
    IUserdService service;

    @GetMapping
    public Object findAll(){
        List<Userd> userds = service.list();
        return ResultUtil.success(userds);
    }

    @PostMapping
    public Object insertUser(@RequestBody Userd userd){
        boolean save = service.save(userd);
        if(save == true){
            return ResultUtil.success("添加成功");
        }else{
            return ResultUtil.error(800,"添加失败");
        }
    }
    @PutMapping
    public Object updateUser(@RequestBody Userd userd){
        boolean update = service.updateById(userd);
        if(update == true){
            return ResultUtil.success("更新成功");
        }else{
            return  ResultUtil.error(800,"更新失败");
        }
    }
    @DeleteMapping
    public Object deleteUser(Integer id){
        boolean remove = service.removeById(id);
        if (remove == true){
            return ResultUtil.success("删除成功");
        }else{
            return  ResultUtil.error(800,"删除失败");
        }
    }
    //@GetMapping()
    public Object findAllUserByCount(@RequestBody UserQuery query){
        Page<Userd> OPage = new Page<>(query.getPageNum(), query.getPageSize());
        QueryWrapper<Userd> wrapper=new QueryWrapper<>();
//        if (query.getPageNum() != null && !query.getUsername().equals("")){
//            wrapper.like("username",query.getUsername());
//        }
//        if (query.getRoleId()!=null){
//            wrapper.eq("roleId",query.getRoleId());
//        }
        Page<Userd> page = service.page(OPage, wrapper);
        System.out.println("总条数："+page.getTotal());
        return ResultUtil.success(page);
    }

    //@GetMapping
    public Object getAllUser(@RequestBody UserQuery userQuery){
        Page<Userd> page=new Page<>(userQuery.getPageNum(),userQuery.getPageSize());
        QueryWrapper<Userd> wrapper=new QueryWrapper<>();
        Page<Userd> page1 = service.page(page, wrapper);

        return ResultUtil.success(page1);
    }

}
